﻿Imports System
Imports System.IO
Imports System.Data.OleDb
Imports System.Data
Imports System.Drawing
Imports System.Drawing.Imaging

Namespace Arwani
    Partial Class Backend_EditCategory
        Inherits System.Web.UI.Page

        Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
            If Me.txtCategoryName.Text = "" Then Me.lblError.Text = "Please enter category name" : Exit Sub
            Dim arwCon As New ArwaniController
            Dim strQ As String
            If CInt(Request.QueryString("catid")) = 0 Then
                'If Me.fileImage.PostedFile.FileName <> "" Then
                '    Dim ext As String = IO.Path.GetExtension(Me.fileImage.PostedFile.FileName)
                '    Dim strFileName As String = Guid.NewGuid.ToString() & ext
                '    Me.fileImage.PostedFile.SaveAs(Server.MapPath("../Category/" & strFileName))
                '    Call ResizeImages(strFileName, "Thumb_" & strFileName)

                '    strQ = "insert into  category (catname,catpicture,divisionid) values ('" & Me.txtCategoryName.Text & "','" & strFileName & "'," & CInt(Request.QueryString("divid")) & ")"
                'Else
                strQ = "insert into  category (catname,divisionid,catpicture,sortorder) values ('" & Me.txtCategoryName.Text & "'," & CInt(Request.QueryString("divid")) & ",''," & CInt(Me.txtSortOrder.Text) & ")"
                'End If
                arwCon.ExecuteNonQuery(strQ)
            Else
                'If Me.fileImage.PostedFile.FileName <> "" Then
                '    Dim ext As String = IO.Path.GetExtension(Me.fileImage.PostedFile.FileName)
                '    Dim strFileName As String = Guid.NewGuid.ToString() & ext
                '    Me.fileImage.PostedFile.SaveAs(Server.MapPath("../Category/" & strFileName))
                '    Call ResizeImages(strFileName, "Thumb_" & strFileName)

                '    strQ = "update category set catname='" & Me.txtCategoryName.Text & "',catpicture='" & strFileName & "',divisionid=" & CInt(Request.QueryString("divid")) & " where catid=" & CInt(Request.QueryString("catid"))
                'Else
                strQ = "update category set catname='" & Me.txtCategoryName.Text & "',divisionid=" & CInt(Request.QueryString("divid")) & ",sortorder=" & CInt(Me.txtSortOrder.Text) & "  where catid=" & CInt(Request.QueryString("catid"))
                'End If
                arwCon.ExecuteNonQuery(strQ)
            End If

            Response.Redirect("ManageCategory.aspx?divid=" & CInt(Request.QueryString("divid")))
        End Sub
        Sub ResizeImages(ByVal FileName, ByVal NewFileName)
            Dim intWidth, intHeight As Integer

            intWidth = 730 '*** Fix Width ***'  
            intHeight = 248   '*** If = 0 Auto Re-Cal Size ***'  

            Dim objGraphic As System.Drawing.Image = System.Drawing.Image.FromFile(Server.MapPath("../Category/" & FileName))

            Dim objBitmap As Bitmap
            '*** Calculate Height ***'  
            If intHeight > 0 Then
                objBitmap = New Bitmap(objGraphic, intWidth, intHeight)
            Else
                If objGraphic.Width > intWidth Then
                    Dim ratio As Double = objGraphic.Height / objGraphic.Width
                    intHeight = ratio * intWidth
                    objBitmap = New Bitmap(objGraphic, intWidth, intHeight)
                Else
                    objBitmap = New Bitmap(objGraphic)
                End If
            End If

            '*** Save As  ***'  
            objBitmap.Save(Server.MapPath("../Category/" & NewFileName), objGraphic.RawFormat)

            '*** Close ***'  
            objGraphic.Dispose()

            '*** Nothing ***'  
            objBitmap = Nothing
            objGraphic = Nothing

        End Sub
        Protected Sub cmdBack_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdBack.Click
            Response.Redirect("ManageCategory.aspx?divid=" & CInt(Request.QueryString("divid")))
        End Sub

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If (Session("ADMIN_USER") IsNot Nothing) Then
                If Page.IsPostBack = True Then Exit Sub
                If Request.QueryString("catid") <> "0" Then
                    Call loadData()
                End If
            Else
                Response.Redirect("Default.aspx")
            End If

        End Sub
        Public Sub loadData()
            Dim arwCon As New ArwaniController
            Dim strQ As String = "select * from category where catid=" & CInt(Request.QueryString("catid"))
            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
            If dtTable.Rows.Count > 0 Then
                If Not IsDBNull(dtTable.Rows(0)("catname")) Then Me.txtCategoryName.Text = dtTable.Rows(0)("catname")
                If Not IsDBNull(dtTable.Rows(0)("sortorder")) Then Me.txtSortOrder.Text = dtTable.Rows(0)("sortorder")
            End If
        End Sub
    End Class
End Namespace